package com.javasm.service.impl;

import com.javasm.dao.AdminUserDao;
import com.javasm.entity.AdminUser;
import com.javasm.service.UserService;
import com.javasm.utils.MyBatisHeapler;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 0.1
 * @className: UserServiceImpl
 * @description:
 * @author: gfs
 * @date: 2025/6/26 9:51
 * @since: jdk17
 */
public class UserServiceImpl implements UserService {

    @Override
    public List<AdminUser> listUser(AdminUser inputUser, Integer page, Integer pageSize) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        AdminUserDao mapper = sqlSession.getMapper(AdminUserDao.class);
        List<AdminUser> adminUsers = mapper.listUser(inputUser, (page-1)*pageSize, pageSize);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return adminUsers;
    }

    @Override
    public Integer countUser(AdminUser inputUser) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        AdminUserDao mapper = sqlSession.getMapper(AdminUserDao.class);
        Integer totalNum = mapper.countUser(inputUser);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return totalNum;
    }

    @Override
    public Integer insertUser(AdminUser inputUser) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        AdminUserDao mapper = sqlSession.getMapper(AdminUserDao.class);
        Integer resNum = mapper.insertUser(inputUser);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return resNum;
    }

    @Override
    public Integer updateUser(AdminUser inputUser) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        AdminUserDao mapper = sqlSession.getMapper(AdminUserDao.class);
        Integer resNum = mapper.updateUser(inputUser);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return resNum;
    }

    @Override
    public Integer deleteUser(List<Long> listUid) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        AdminUserDao mapper = sqlSession.getMapper(AdminUserDao.class);
        Integer resNum = mapper.deleteUser(listUid);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return resNum;
    }

    @Override
    public Boolean updateUserAuth(List<Long> listMid, Long uid) {
        //情况: 删除 >0  添加 >0   成功
        //     删除  0  添加 >0   成功
        //     删除 >0  添加 0    成功
        //     删除  0  添加 0    成功
        //              添加需要>0 实际有错

        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        AdminUserDao mapper = sqlSession.getMapper(AdminUserDao.class);
        mapper.deleteUserMenu(uid);
        //如果listMid没值 sql需要跳过 不执行
        Boolean flag = true;
        if(listMid != null){
           Integer resNum =  mapper.insertUserMenu(listMid,uid);
           if(resNum==0){
               flag = false;
           }
        }

        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return flag;
    }
}
